package com.lindroy.androidplayer.ui.home.fragment

import android.os.Bundle
import android.widget.ImageView
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.BaseLazyFragment
import com.lindroy.androidplayer.base.appViewModel
import com.lindroy.androidplayer.base.eventViewModel
import com.lindroy.androidplayer.extensions.*
import com.lindroy.androidplayer.logic.model.WXArticleResp
import com.lindroy.androidplayer.logic.model.bean.CollectBus
import com.lindroy.androidplayer.ui.adapter.WxArticleAdapter
import com.lindroy.androidplayer.ui.webview.ContentActivity
import com.lindroy.androidplayer.viewmodel.WXArticleViewModel
import com.lindroy.androidplayer.viewmodel.CollectViewModel
import kotlinx.android.synthetic.main.fragment_wx_article.*
import kotlinx.android.synthetic.main.fragment_wx_article.refreshLayout

/**
 * @author Lin
 * @date 2021/4/9
 * @function 公众号文章列表
 * @Description
 */
private const val KEY_CHAPTER_ID = "key_chapter_id"

class WXArticleFragment(override val layoutId: Int = R.layout.fragment_wx_article) :
    BaseLazyFragment() {

    private val wxArticleViewMode: WXArticleViewModel by lazy { ViewModelProvider(this)[WXArticleViewModel::class.java] }

    private val collectViewModel: CollectViewModel by viewModels()

    private val adapter: WxArticleAdapter by lazy {
        WxArticleAdapter()
    }

    override fun initArguments(bundle: Bundle) {
        super.initArguments(bundle)
        wxArticleViewMode.chapterId = bundle.getInt(KEY_CHAPTER_ID)
    }

    override fun initView() {
        refreshHeader.initColor()
        rvWXArticle.setLineDivider()
        rvWXArticle.adapter = adapter
        addLoadingObserve(collectViewModel)
    }

    override fun lazyLoad() {
        stateLayout.showLoading()
        wxArticleViewMode.reqWXArticles(true)
    }

    override fun initListener() {
        refreshLayout.setOnRefreshListener {
            wxArticleViewMode.reqWXArticles(true)
        }
        refreshLayout.setOnLoadMoreListener {
            wxArticleViewMode.reqWXArticles(false)
        }
        collectViewModel.collectState.observe(this) {
            it.success.yes {
                eventViewModel.collectEvent.value = CollectBus(it.id, it.collect, it.originId)
            }
        }
        wxArticleViewMode.wxArticleLiveData.observe(this) {
            loadListData(it, adapter, refreshLayout, stateLayout)
        }
        eventViewModel.collectEvent.observeInFragment(this) { collectBus ->
            val index = adapter.data.indexOfFirst {
                it.id == collectBus.id
            }
            if (index >= 0) {
                adapter.data[index].collect = collectBus.collect
                adapter.notifyItemChanged(index)
            }
        }

        appViewModel.loginInfo.observeInFragment(this){
            judgeNotLogin {
                //退出登录的话把收藏都去掉
                adapter.data.forEach {
                    it.collect.yes {
                        it.collect = false
                    }
                }
                adapter.notifyDataSetChanged()
            }
        }
    }

    override fun initClick() {
        adapter.setOnCollectClickListener { collect, id ->
            collectViewModel.toggleCollectListArticle(collect, id)
        }
        adapter.setOnItemClickListener {
            context?.apply {
                ContentActivity.start(this,it.id,it.link,it.title,it.collect)
            }
        }
        stateLayout.setOnViewsClickListener { state, view ->

        }
    }

    companion object {
        @JvmStatic
        fun newInstance(chapterId: Int) =
            WXArticleFragment().apply {
                arguments = Bundle().apply {
                    putInt(KEY_CHAPTER_ID, chapterId)
                }
            }
    }
}